package de.sisy.RemoteFileManager;

import se.sisy.Connection.SshConnection;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.lamerman.FileDialog;

public class SettingsActivity extends Activity {
	/** Called when the activity is first created. */
	static final int REQUEST_SAVE = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
				.permitAll().build();
		StrictMode.setThreadPolicy(policy);

		setContentView(R.layout.main);

		Button runButton = (Button) findViewById(R.id.button1);
		runButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					connectssh();
				} catch (JSchException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SftpException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Button browseButton = (Button) findViewById(R.id.button2);
		browseButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent(SettingsActivity.this,
						RemoteFileBrowserActivity.class);
				startActivity(intent);
			}
		});
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// onActivityResult f�r den local Filemanager
		if (requestCode == REQUEST_SAVE) {
			if (resultCode == RESULT_OK) {
				// Dateipfad der ausgew�hlten Datei
				String pickedfilepath = data
						.getStringExtra(FileDialog.RESULT_PATH);
				System.out.println(pickedfilepath);

				Context context = getApplicationContext();
				CharSequence text = pickedfilepath;
				int duration = Toast.LENGTH_SHORT;

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
			}
		}
	}

	private void connectssh() throws JSchException, SftpException {
		SshConnection connection = SshConnection.getInstance();
		JSch jsch = connection.getJsch();

		String knownHostsFilename = "/home/username/.ssh/known_hosts";
		jsch.setKnownHosts(knownHostsFilename);

		String remoteUsername = ((EditText) (findViewById(R.id.tf_userName)))
				.getText().toString();
		String remoteHost = ((EditText) (findViewById(R.id.tf_remoteHost)))
				.getText().toString();
		String password = ((EditText) (findViewById(R.id.tf_password)))
				.getText().toString();

		Session session = jsch.getSession(remoteUsername, remoteHost, 22);

		java.util.Properties config = new java.util.Properties();
		// schauen wo die known_hosts datei liegt...
		// liegt in Android wo anders, desswegen check vorerst aus geschaltet
		config.put("StrictHostKeyChecking", "no");
		session.setConfig(config);

		session.setPassword(password);

		session.connect();

		Channel channel = session.openChannel("sftp");
		channel.connect();

		ChannelSftp sftpChannel = (ChannelSftp) channel;

		// hier die shell befehle ausführen
		// nur welche die als Methode von ChannelSftp implementiert werden gehen
		// (vllt anders andere auch)

		connection.setChannel(sftpChannel);

		((TextView) findViewById(R.id.tv_status))
				.append("Successfully connected!!");

	}

}